package com.wanda.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.sound.midi.SysexMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import com.wanda.model.CustomerModel;
import com.wanda.model.JsonGeneralModel;
import com.wanda.model.JsonModelGeneral;
import com.wanda.model.Wd_RoleModel;
import com.wanda.service.CustomerService;
import com.wanda.service.Wd_RoleService;
import com.wanda.util.DateFormat;


@RequestMapping(value="/role")
@Controller
public class Wd_RoleController {
	@Resource
	private Wd_RoleService wd_RoleService;
	

	//添加角色
	@RequestMapping(value="/addRole.do")
    public @ResponseBody Map<String,Object> add(HttpServletRequest request){
		System.out.println("添加成功"); 
		String role_name=request.getParameter("role_name");
		Wd_RoleModel model = new Wd_RoleModel();
        model.setRole_name(role_name);		
        model.setDelete_state("0");
        wd_RoleService.addRole(model);
    	Map<String,Object> map = new HashMap<String,Object>();  
    	map.put("msg","成功");
    	return map;
    }
	
	
	//修改角色
		@RequestMapping(value="/updateRole.do")
	    public @ResponseBody Map<String,Object> update(String role_id,String role_name,String update_user){
            Wd_RoleModel role = new Wd_RoleModel();
            role.setRole_id(Integer.parseInt(role_id));
            role.setRole_name(role_name);
            role.setUpdate_user(update_user);
            role.setUpdate_time(DateFormat.getNowDate());      
           int row= wd_RoleService.updateRole(role);
	    	Map<String,Object> map = new HashMap<String,Object>();  
	    	map.put("msg", row);
	    	return map;
	    }
		
		
		//删除角色
		@RequestMapping(value="/deleteRole.do")
			 public @ResponseBody Map<String,Object> delete(String role_id,String delete_user){
				    
			        Wd_RoleModel role = new Wd_RoleModel();
			        role.setRole_id(Integer.parseInt(role_id));
			        String delete_state="1"; 
			        role.setDelete_user(delete_user);
			        role.setDelete_state(delete_state);
			        role.setDelete_time(DateFormat.getNowDate());
			        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"+role_id);
			        wd_RoleService.deleteRole(role);
			    	Map<String,Object> map = new HashMap<String,Object>();  
			    	System.out.println("删除成功"); 
			    	;
			    	return map;
			    }

		//按照ID查询角色
			@RequestMapping(value="/selectRoleById.do")
			  public ModelAndView selectRoleById(String role_id){				
				
				Wd_RoleModel role = wd_RoleService.selectById(Integer.parseInt(role_id));
				ModelAndView model = new ModelAndView();
				model.setViewName("待定");
				model.addObject("role", role);
				return model;
				}
				 
		  //查询所有角色
				 @RequestMapping(value="/selectRoleAll.do")
				    public @ResponseBody JsonModelGeneral  selectAll(){
					 List<Wd_RoleModel> roleList = wd_RoleService.selectAll();
					 ModelAndView model = new ModelAndView();
 					// model.setViewName("listrole.jsp");
						JsonModelGeneral json = new JsonModelGeneral();
					    json.setRows(roleList);
					 model.addObject("roleList", roleList);
					 System.out.println(roleList.size()+roleList.get(0).getRole_name());
					 return json;
	 
				    }
		
}
